package com.longtop.leaning.ui;

import java.util.Date;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

import com.longtop.leaning.business.Query;
import com.longtop.leaning.data.AppData;
import com.longtop.leaning.data.UIData;

public class MainForm extends Form implements CommandListener {

	private static TextField fltnoTextField = null;
	private static StringItem dateStringItem = null;
	private static DateField fltdateStartTextField = null;
	private static DateField fltdateEndTextField = null;
	private static ChoiceGroup fltStatusChoiceGroup = null;
	private static StringItem noteItem = null;

	public MainForm() {
		super("航班查询");
		fltnoTextField = new TextField("  航班号：", "", 15, TextField.ANY);
		dateStringItem = new StringItem("起飞时间：", "");
		fltdateStartTextField = new DateField("自：", DateField.DATE_TIME);
		fltdateEndTextField = new DateField("到：", DateField.DATE_TIME);
		String[] stringArray = { "全部", "正常", "延误", "取消" };//0 正常 1 延误 2 取消
		fltStatusChoiceGroup = new ChoiceGroup("航班状态：", ChoiceGroup.POPUP, stringArray, null);
		this.append(fltnoTextField);
		this.append(dateStringItem);
		this.append(fltdateStartTextField);
		this.append(fltdateEndTextField);
		this.append(fltStatusChoiceGroup);
		super.addCommand(UIData.CMD_BACK);
		super.addCommand(UIData.CMD_OK);
		super.setCommandListener(this);
	}

	public void commandAction(Command c, Displayable d) {
		if (c == UIData.CMD_OK) {
			String fltno = fltnoTextField.getString().trim();
			Date fltdateStart = fltdateStartTextField.getDate();
			Date fltdateEnd = fltdateEndTextField.getDate();
			int fltstatus = fltStatusChoiceGroup.getSelectedIndex() -1;
			
			if(noteItem == null){ 
				noteItem = new StringItem("", "查询中，请稍候。。。");
				this.append(noteItem);
			}
			
			System.out.println("query flights parameters: ");
			System.out.println("fltno: " + fltno);
			System.out.println("fltdateStart: " + fltdateStart);
			System.out.println("fltdateEnd: " + fltdateEnd);
			System.out.println("fltstatus: " + fltstatus);
			
			if(Query.query(fltno, fltdateStart, fltdateEnd, fltstatus)){
				noteItem.setText("查询成功！");
				UIData.display.setCurrent(new FltList());
			}else{
				noteItem.setText("查询失败！");
			}
			
		}
		if (c == UIData.CMD_BACK) {
			if (AppData.isOnLine) {
				UIData.display.setCurrent(new FunctionList());
			} else {
				UIData.display.setCurrent(new LoginForm());
			}
		}
	}

}
